package com.newhopegroup.webcommons.utils;

import com.google.common.collect.Lists;

import java.nio.ByteBuffer;
import java.util.List;

/**
 * Created by wangshida on 15/11/4.
 * 聪明的ByteByffer
 */
public class SmartByteBuffer {
    private List<byte[]> buffer = Lists.newArrayList();

    public void put(byte[] item) {
        buffer.add(item);
    }

    public byte[] array() {
        int totalSize = buffer.parallelStream().mapToInt(bytes -> bytes.length).sum();
        ByteBuffer result = ByteBuffer.allocate(totalSize);
        buffer.forEach(bytes -> result.put(bytes));
        return result.array();
    }
}
